import ElementUI from 'element-ui'

/* 提供一个重新设置rules的方法 */
export const HighElFormItem = {
    name: 'HighElFormItem',
    extends: ElementUI.FormItem,
    data() {
        this.customRules = null
        return {}
    },
    methods: {
        // 暂时没删除啊...
        setCustomRules(rules) {
            if (!this.customRules) this.customRules = []
            if (rules) {
                this.customRules = this.customRules.concat(rules) // 注意此方法设置的规则优先级较高
            }
        },
    },
    created() {
        const getRules = this.getRules
        this.getRules = () => {
            let rules = getRules()
            rules = !Array.isArray(this.customRules) ? rules : rules.concat(this.customRules)
            return rules
        }
    },
}


export const HighRender = {
    functional: true,
    props: {
        render: Function,
    },
    render(h, context) {
        return context.props.render(h, context)
    },
}


export const SlotRender = {
    functional: true,
    inject: ['dForm'],
    props: {
        name: String, // 对应v-slot:name
    },
    render(h, context) {
        const allSlotRender = context.injections.dForm.$scopedSlots
        for (let k in allSlotRender) {
            if (k === context.props.name) {
                return allSlotRender[k](context)
            }
        }
        return h('el-alert', {
            attrs: {
                type: 'success',
                title: '自定义内容',
                style: 'background-color:#f0f9eb;', // fix 效果不一致
            },
        })
    },
}
